﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PersistSaveTests.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.Tests
{
  using System;
  using System.Linq;

  using DataDuctus.Snmp.Persistor.Core.Extensions;
  using DataDuctus.Snmp.Persistor.Core.Persistor;
  using DataDuctus.Snmp.Persistor.Core.Tests.Internal;
  using DataDuctus.Snmp.Persistor.Core.Tests.TestData;

  using Lextm.SharpSnmpLib;

  using Microsoft.VisualStudio.TestTools.UnitTesting;

  /// <summary>
  /// Test persistence.
  /// </summary>
  [TestClass]
  public class PersistSaveTests : NetSnmpAgentBasedTest
  {
    #region Public Methods and Operators

    /// <summary>
    /// The test add route.
    /// </summary>
    /// <exception cref="Exception">Route already added.</exception>
    [TestMethod]
    [Ignore]
    public void TestAddRoute()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        if (
          session.Load<Route>().Any(
            route =>
            route.PrimaryMetric == 77777 && route.Age == 77777 && route.Destination == "192.168.221.0".ToIpAddress()
            && route.Type == RouteType.Invalid))
        {
          throw new Exception("Already added, need to be removed first");
        }
      }

      using (EntitySession session = factory.OpenSession())
      {
        session.Save(
          new Route
            {
              Age = 77777, 
              AlternativeMetric1 = 0, 
              AlternativeMetric2 = 0, 
              AlternativeMetric3 = 0, 
              AlternativeMetric4 = 0, 
              Destination = "192.168.221.0".ToIpAddress(), 
              Info = new ObjectIdentifier("0.0"), 
              Interface = 1, 
              NetMask = "255.255.255.0".ToIpAddress(), 
              NextHop = "192.168.221.0".ToIpAddress(), 
              Protocol = ProtocolType.Netmgmt, 
              Type = RouteType.Invalid, 
              PrimaryMetric = 77777
            });
      }
    }

    /// <summary>
    /// The test set contact.
    /// </summary>
    [TestMethod]
    public void TestSetContact()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      SystemInfo sysInfo;

      using (EntitySession session = factory.OpenSession())
      {
        sysInfo = session.Load<SystemInfo>().First();
      }

      string contact = sysInfo.Contact;

      using (EntitySession session = factory.OpenSession())
      {
        sysInfo.Contact = "hello world";
        session.Save(sysInfo);
      }

      using (EntitySession session = factory.OpenSession())
      {
        SystemInfo sysInfo2 = session.Load<SystemInfo>().First();

        if (sysInfo2.Contact != "hello world")
        {
          Console.WriteLine("should contain hello world");
        }
      }

      using (EntitySession session = factory.OpenSession())
      {
        sysInfo.Contact = contact;
        session.Save(sysInfo);
      }
    }

    #endregion
  }
}